package com.healthcloud.controller;
/**
 * 慢病数据接口控制类
 */

import com.healthcloud.entity.*;
import com.healthcloud.service.ChronicService;
import com.healthcloud.service.SignPatientService;
import com.healthcloud.service.UserService;
import com.healthcloud.utils.EncryptUtil;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.PageParam;
import com.healthcloud.utils.ShiroUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@ApiIgnore
@Controller
@RequestMapping(value = "/chronic")
public class ChronicController {

	@Autowired
	private UserService userService;

	@Autowired
	private ChronicService chronicService;

	@Autowired
	private SignPatientService signService;

/*	@RequestMapping(value = "/getBpMgr")
	public String getBpList(Model model, @Param("username") String username, @Param("pwd") String pwd){
		model.addAttribute("pwd",pwd);
		model.addAttribute("user_name",username);
		Subject subject = SecurityUtils.getSubject();
		//MD5加密
		pwd = new EncryptUtil().encryption(pwd);
		UsernamePasswordToken token = new UsernamePasswordToken(username, pwd);
		subject.login(token);
		SysUserEntity userEntity = (SysUserEntity)SecurityUtils.getSubject().getPrincipal();
		SysUserEntity user = userService.get(userEntity.getId());
		model.addAttribute("username",user.getRealName());
		return "newframe/chronic/bplist";
	}*/

	@RequestMapping(value = "/getBpMgr")
	public String getBpList(Model model, String username, String pwd){

		SysUserEntity user = userService.get(ShiroUtils.getUserId());
		if("superadmin".equals(user.getUsername())) {
			username = "13111111111";
			pwd = new EncryptUtil().encryption("123456");
		}else {
			username = user.getUsername();
			pwd = user.getPassword();
		}

		model.addAttribute("pwd",pwd);
		model.addAttribute("user_name",username);
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(username, pwd);
		subject.login(token);

		SysUserEntity userEntity = (SysUserEntity)SecurityUtils.getSubject().getPrincipal();
		SysUserEntity sysUserEntity = userService.get(userEntity.getId());
		System.out.println("1");
		model.addAttribute("username",sysUserEntity.getRealName());
		return "newframe/chronic/bplist";
	}

	@RequestMapping(value = "/searchPatient")
	public @ResponseBody JSONObject searchPatient(String name,String pageNumber,String pageSize) {

		PatientBean bean = new PatientBean();
		SysUserEntity userEntity = (SysUserEntity)SecurityUtils.getSubject().getPrincipal();

		bean.setUserId(Integer.parseInt(userEntity.getId().toString()));
		if(name!=null && !"".equals(name)){
			bean.setName(name.toUpperCase());
		}
		int iIndex = Integer.parseInt(pageSize)*(Integer.parseInt(pageNumber)-1);
		List<SignPatientBean> list = chronicService.searchPatient(bean,String.valueOf(iIndex),pageSize);
		int count = chronicService.countPatient(bean);
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("rows", JSONArray.fromObject(list));//JSONArray
		jsonObject.put("total",count);//总记录数
		return jsonObject;
	}

	@RequestMapping(value = "/searchBpExp")
	public @ResponseBody JSONObject searchBpExp(String pId) {

		TestResultExpBean bean = new TestResultExpBean();
		bean.setTestItemCode("104001,104002");
		bean.setpId(pId);
		List<TestResultExpBean> list = chronicService.searchResultExp(bean);
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("rows", JSONArray.fromObject(list));//JSONArray
		//jsonObject.put("total",count);//总记录数
		return jsonObject;

	}

	@RequestMapping(value = "/searchDueFollowUpPlan")
	public @ResponseBody JSONObject searchDueFollowUpPlan() {
		DueFollowUpPlanBean bean = new DueFollowUpPlanBean();
		SysUserEntity userEntity = (SysUserEntity)SecurityUtils.getSubject().getPrincipal();

		bean.setUserId(Integer.parseInt(userEntity.getId().toString()));
		List<DueFollowUpPlanBean> list = chronicService.searchDueFollowUpPlan(bean);
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("rows", JSONArray.fromObject(list));//JSONArray
		//jsonObject.put("total",count);//总记录数
		return jsonObject;
	}

	@RequestMapping(value = "/getBpList",method=RequestMethod.GET)
	public String getBpMgr(Model model,String idCard,@Param("username") String username,@Param("pwd") String pwd){

		model.addAttribute("pwd",pwd);
		model.addAttribute("user_name",username);
		Subject subject = SecurityUtils.getSubject();
		//MD5加密
		pwd = new EncryptUtil().encryption(pwd);
		UsernamePasswordToken token = new UsernamePasswordToken(username, pwd);
		subject.login(token);


		SysUserEntity userEntity = (SysUserEntity)SecurityUtils.getSubject().getPrincipal();
		SysUserEntity user = userService.get(userEntity.getId());

		model.addAttribute("username",user.getRealName());
		model.addAttribute("idCard", idCard);
		//根据idCard获取血压方案
		//查询最近7次血压数据生成折线图
		ChronicTestResultBean bean = new ChronicTestResultBean();
		bean.setIdCard(idCard);
		//收缩压数据
		bean.setTestItemCode("104001");
		List<ChronicTestResultBean> list1 = chronicService.searchTestResult(bean);
		model.addAttribute("systolicList", JSONArray.fromObject(list1));
		//舒张压数据
		bean.setTestItemCode("104002");
		List<ChronicTestResultBean> list2 = chronicService.searchTestResult(bean);
		model.addAttribute("diastolicList", JSONArray.fromObject(list2));
		//血压建议方案
		SignPatientBean patientBean = new SignPatientBean();
		patientBean.setPatientIdCard(idCard);
		List<SignPatientBean>	signPatientBeans =	signService.signSearch(patientBean,0,10);
		if(signPatientBeans != null && !signPatientBeans.isEmpty()){
			SignPatientBean signPatientBean = signPatientBeans.get(0);
			model.addAttribute("signPatientBean",signPatientBean);
			List<BloodManager> list = chronicService.queryBloodByPId(signPatientBean.getpId(),"0");
			if(list != null && !list.isEmpty()){
				model.addAttribute("bloodManager",list.get(0));
			}
		}

		return "newframe/chronic/bpmgr";
	}

	@RequestMapping(value = "/searchBpList")
	public @ResponseBody JSONObject searchBpList(ChronicTestResultBean bean,PageParam pageParam) {
		/*TestResultExpBean bean = new TestResultExpBean();
		bean.setTestItemCode("104001,104002");
		bean.setpId(pId);*/
		List<ChronicTestResultBean> list = chronicService.searchBpList(bean,pageParam);
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("rows", JSONArray.fromObject(list));//JSONArray

		int count = chronicService.countBpList(bean);
		jsonObject.put("total",count);//总记录数
		return jsonObject;
	}

	@RequestMapping(value = "/getBsMgr",method=RequestMethod.GET)
//	@RequestMapping(value = "/getBsList",method=RequestMethod.GET)
	public String getBsList(Model model, String username, String pwd){

		//todo
		SysUserEntity user = userService.get(ShiroUtils.getUserId());
		if("superadmin".equals(user.getUsername())) {
			username = "13111111111";
			pwd = new EncryptUtil().encryption("123456");
		}else {
			pwd = user.getPassword();
			username = user.getUsername();
		}

		model.addAttribute("pwd",pwd);
		model.addAttribute("user_name",username);
		Subject subject = SecurityUtils.getSubject();

		UsernamePasswordToken token = new UsernamePasswordToken(username, pwd);
		subject.login(token);

		/***********************************************************************/
		SysUserEntity userEntity = (SysUserEntity)SecurityUtils.getSubject().getPrincipal();
		SysUserEntity entity = userService.get(userEntity.getId());

		model.addAttribute("username",entity.getRealName());
		return "newframe/chronic/bslist";
	}

	@RequestMapping(value = "/toError")
	public String toError(){
		return "newframe/error";
	}

	@RequestMapping(value = "/searchBsExp")
	public @ResponseBody JSONObject searchBsExp(String pId) {
		TestResultExpBean bean = new TestResultExpBean();
		bean.setTestItemCode("105001,105002");
		bean.setpId(pId);
		List<TestResultExpBean> list = chronicService.searchResultExp(bean);
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("rows", JSONArray.fromObject(list));//JSONArray
		//jsonObject.put("total",count);//总记录数
		return jsonObject;
	}

	@RequestMapping(value = "/getBsList",method=RequestMethod.GET)
//	@RequestMapping(value = "/getBsMgr",method=RequestMethod.GET)
	public String getBsMgr(Model model,@Param("idCard") String idCard,@Param("username") String username,@Param("pwd") String pwd){

		model.addAttribute("pwd",pwd);
		model.addAttribute("user_name",username);
		Subject subject = SecurityUtils.getSubject();
		//MD5加密
		pwd = new EncryptUtil().encryption(pwd);
		UsernamePasswordToken token = new UsernamePasswordToken(username, pwd);
		subject.login(token);
		/*********************************************************************/
		SysUserEntity userEntity = (SysUserEntity)SecurityUtils.getSubject().getPrincipal();
		SysUserEntity user = userService.get(userEntity.getId());
		model.addAttribute("username",user.getRealName());
		model.addAttribute("idCard", idCard);
		//根据idCard获取血压方案
		//查询最近7次血压数据生成折线图
		ChronicTestResultBean bean = new ChronicTestResultBean();
		bean.setIdCard(idCard);
		//空腹血糖
		bean.setTestItemCode("105001");
		List<ChronicTestResultBean> list1 = chronicService.searchTestResult(bean);
		model.addAttribute("systolicList", JSONArray.fromObject(list1));
		//餐后2小时血糖
		bean.setTestItemCode("105002");
		List<ChronicTestResultBean> list2 = chronicService.searchTestResult(bean);
		model.addAttribute("diastolicList", JSONArray.fromObject(list2));
		//血糖建议方案
		SignPatientBean patientBean = new SignPatientBean();
		patientBean.setPatientIdCard(idCard);
		List<SignPatientBean>	signPatientBeans =	signService.signSearch(patientBean,0,10);
		if(signPatientBeans != null && !signPatientBeans.isEmpty()){
			SignPatientBean signPatientBean = signPatientBeans.get(0);
			model.addAttribute("signPatientBean",signPatientBean);
			List<BloodManager> list = chronicService.queryBloodByPId(signPatientBean.getpId(),"1");
			if(list != null && !list.isEmpty()){
				model.addAttribute("bloodManager",list.get(0));
			}
		}

		return "newframe/chronic/bsmgr";
	}
	@RequestMapping(value = "/searchBsList")
	public @ResponseBody JSONObject searchBsList(ChronicTestResultBean bean,PageParam pageParam) {
		/*TestResultExpBean bean = new TestResultExpBean();
		bean.setTestItemCode("104001,104002");
		bean.setpId(pId);*/
		List<ChronicTestResultBean> list = chronicService.searchBsList(bean,pageParam);
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("rows", JSONArray.fromObject(list));//JSONArray

		int count = chronicService.countBsList(bean);
		jsonObject.put("total",count);//总记录数
		return jsonObject;
	}

	@RequestMapping(value = "/getSmartList",method=RequestMethod.GET)
	public String getSmartList(Model model,@Param("username") String username,@Param("pwd") String pwd){

		model.addAttribute("pwd",pwd);
		model.addAttribute("user_name",username);
		Subject subject = SecurityUtils.getSubject();
		//MD5加密
		pwd = new EncryptUtil().encryption(pwd);
		UsernamePasswordToken token = new UsernamePasswordToken(username, pwd);
		subject.login(token);

		SysUserEntity userEntity = (SysUserEntity)SecurityUtils.getSubject().getPrincipal();
		SysUserEntity user = userService.get(userEntity.getId());

		model.addAttribute("username",user.getRealName());
		return "newframe/chronic/smartlist";
	}

	@RequestMapping(value = "/searchSmartList")
	public @ResponseBody JSONObject searchSmartList(SmartDataBean bean,String pageNumber,String pageSize) {
		SysUserEntity userEntity = (SysUserEntity)SecurityUtils.getSubject().getPrincipal();

		bean.setUserId(Integer.valueOf(userEntity.getId().toString()));
		int iIndex = Integer.parseInt(pageSize)*(Integer.parseInt(pageNumber)-1);
		List<SmartDataBean> list = chronicService.searchSmartList(bean, String.valueOf(iIndex),pageSize);
		int total = chronicService.countSmartList(bean);
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("rows", JSONArray.fromObject(list));//JSONArray
		jsonObject.put("total",total);//总记录数
		return jsonObject;
	}

	@RequestMapping(value = "/selectPatient")
	public @ResponseBody
    ApiRes selectPatient(String limit, String offset, String name){

		PatientBean bean = new PatientBean();
		SysUserEntity userEntity = (SysUserEntity)SecurityUtils.getSubject().getPrincipal();

		bean.setUserId(Integer.valueOf(userEntity.getId().toString()));
		if(name!=null && !"".equals(name)){
			bean.setName(name.toUpperCase());
		}
		List<SignPatientBean> list = chronicService.searchPatient(bean,offset,limit);
		int total = chronicService.countPatient(bean);
		return new ApiRes(true,list,null,String.valueOf(total));
	}


	/**
	 * 保存血压血糖方案管理
	 * @param bloodManager
	 * @return
	 */
	@RequestMapping(value = "/saveBlood",method = RequestMethod.POST)
	@ResponseBody
	public ApiRes saveBlood(BloodManager bloodManager){

		if(StringUtils.isBlank(bloodManager.getPatientId())){
			return new ApiRes(false,null,"身份证号码不能为空",null);
		}

		if(bloodManager.getId() == null) {
			chronicService.saveBlood(bloodManager);
		}else{
			chronicService.updateBlood(bloodManager);
		}

		return ApiRes.SUCCESS;

	}



	@RequestMapping(value = "/toBloodMgr",method = RequestMethod.GET)
	@ResponseBody
	public JSONObject showBlood(@Param("pId") String pId,Model model,@Param("type") String type){

		JSONObject jsonObject = new JSONObject();

		List<BloodManager> list = chronicService.queryBloodByPId(Integer.valueOf(pId),type);
//		int total = chronicService.count(Integer.valueOf(pId));

		jsonObject.put("pId",pId);

		if(list != null && !list.isEmpty()){
			jsonObject.put("patientName",list.get(0).getPatientName());
		}

		jsonObject.put("rows",list);

		return jsonObject;

	}

	@RequestMapping(value = "/blood",method = RequestMethod.GET)
	@ResponseBody
	public ApiRes getBlood(@Param("id") String id){

		JSONObject jsonObject=new JSONObject();

		BloodManager bloodManager = chronicService.getById(Integer.valueOf(id));
		jsonObject.put("bloodManager",bloodManager);

		return new ApiRes(true,bloodManager,null,null);
//        return jsonObject;
	}
}
